<?php

class Admin_Category_Controller extends Base_Controller {

    private $_data;
    private $_categoryModel;
	public $restful = true;

    public function __construct() {
        $this->_data = array();
        $this->_categoryModel = new CategoryLogic();
    }

    public function action_index() {
        return View::make('home.index');
    }

    /**
     * 获取顶级目录(root除外)list
     * @return type json string
     */
    public function action_getToplevel() {
        $ret = $this->_categoryModel->getNextLevelCats(0);
        return Response::json($ret);
    }

    public function post_manage(){        
		$id = Input::get('id');
		$parent_id = Input::get('catgory_id');
		$name = Input::get('name');
		
		if( !empty($id)){
			//更新类目
			//$this->_data["state"] = $this->model_news->updateNews($id,$update_array);
			$this->_data["state"]= $this->_categoryModel->updateCategory($id, $name);
		}else{
			//增加类目
			$this->_data["state"]= $this->_categoryModel->addCategory($parent_id, $name);
		}
		if($this->_data["state"] == 1) $this->_data["state"]=true;
		echo Response::json(array("state"=>$this->_data['state']));
	

	}

	public function get_manage(){
		$id = Input::get('id');
		$brothers = array();
        $top_cat = array();

		if(!empty($id)){
			$crumb = $this->_categoryModel->getHieraCats($id);

			if($crumb){
				foreach($crumb as $key => $value){
					$brothers[$value['id']] =$this->_categoryModel->getBrolevelCats($value['id']);
				}
			}
		}else{
			$top_cat=$this->_categoryModel->getNextLevelCats(0);
		}
        $ret = $this->_categoryModel->listCategoryTree();
		return View::make('admin.category.manage')
			->with('data',$ret)
			->with('top_cat', $top_cat)
			->with("brothers",$brothers) 
			->with('id',$id);
	}

    public function action_manage(){        
        $top_cat = $this->_categoryModel->getNextLevelCats(0);
        $ret = $this->_categoryModel->listCategoryTree();
        return View::make('admin.category.manage')->with('data',$ret)->with('top_cat', $top_cat);
    }

    public function action_addCategory() {
        $this->_data["vp"] = $this->validate_parameter();
        $this->_data["input"] = Input::get();

        if ($this->_data["vp"]) {
            $this->_data["state"] = $this->_categoryModel->addCategory(intval($this->_data['input']['parent_id']), $this->_data['input']['name']);
        }

        var_dump($this->_data);
        //view
    }

    public function get_view() {
        $ret = $this->_categoryModel->listCategoryTree();
        return View::make('admin.category.list')->with('data',$ret);
    }

    public function action_updateCategory() {
        $id = Input::get('id');
        foreach (array('name', 'parent', 'sort_order') as $key) {
            $value = Input::get($key);
            if ($value)
                $update_array[$key] = $value;
        }
        $this->_data["state"] = $this->model_category->updateCategory($id, $update_array);

        //var_dump($this->_data);
    }

    public function action_deleteCategory() {
        $id = Input::get('id');
        $this->_data["state"] = $this->model_category->deleteCategory($id);
		echo Response::json(array("state"=>$this->_data['state']));
        //var_dump($this->_data);
    }

    public function action_aalistCategory() {
        $this->_data["category_list"] = $this->model_category->listCategory();
        $parentId_array = array();
        $tree = array();
        foreach ($this->_data["category_list"] as $key => $value) {
            if (!isset($parentId_array[$value->parent_id])) {
                $parentId_array[$value->parent_id] = array();
            }
            array_push($parentId_array[$value->parent_id], $value);
        }
        if (!isset($parentId_array['0'])) {
            return array();
        }
        $stack_array = array();
        foreach ($parentId_array['0'] as $key => $value) {
            $tmp_array = array($value, array());
            $stack_array[] = &$tmp_array;
            $tree[$value->id] = &$tmp_array;
        }
        unset($parentId_array['0']);
        //var_dump($tree);
        while ($tmp = array_pop($stack_array)) {
            //var_dump($tmp);
            if (isset($parentId_array[$tmp[0]->id])) {
                foreach ($parentId_array[$tmp[0]->id] as $key => $value) {
                    $tmp_array = array($value, array());
                    $stack_array[] = &$tmp_array;
                    $tmp[1][] = &$tmp_array;
                }
                //var_dump($stack_array);
                //var_dump($tmp[1]);
                unset($parentId_array[$tmp[0]->id]);
            }
        }

        var_dump($tree);

        //var_dump($this->_data);
    }

    public function action_getCategory() {
        $id = Input::get('id');
        $this->_data["category"] = $this->model_category->getCategory($id);
        var_dump($this->_data);
    }

    private function validate_parameter() {
        /*
          31   `parent_id` int(11) unsigned NOT NULL DEFAULT '0',
          32   `sort_order` int(11) unsigned NOT NULL DEFAULT '0',
          33   `name` varchar(64) NOT NULL DEFAULT '',
         */
        $input = Input::get();
        $rules = array(
            'parent_id' => 'required|max:50',
            'name' => 'required',
                //'sort_order' => 'required'
        );

        $validation = Validator::make($input, $rules);

        if ($validation->fails()) {
            return $validation->errors;
        }

        return true;
    }

}
